home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_sets.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2005-10-18  |  37KB  |  1,076 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. import unittest
  5. import operator
  6. import copy
  7. import pickle
  8. import random
  9. from sets import Set, ImmutableSet
  10. from test import test_support
  11. empty_set = Set()
  12.  
  13. class TestBasicOps(unittest.TestCase):
  14.     
  15.     def test_repr(self):
  16.         if self.repr is not None:
  17.             self.assertEqual(repr(self.set), self.repr)
  18.         
  19.  
  20.     
  21.     def test_length(self):
  22.         self.assertEqual(len(self.set), self.length)
  23.  
  24.     
  25.     def test_self_equality(self):
  26.         self.assertEqual(self.set, self.set)
  27.  
  28.     
  29.     def test_equivalent_equality(self):
  30.         self.assertEqual(self.set, self.dup)
  31.  
  32.     
  33.     def test_copy(self):
  34.         self.assertEqual(self.set.copy(), self.dup)
  35.  
  36.     
  37.     def test_self_union(self):
  38.         result = self.set | self.set
  39.         self.assertEqual(result, self.dup)
  40.  
  41.     
  42.     def test_empty_union(self):
  43.         result = self.set | empty_set
  44.         self.assertEqual(result, self.dup)
  45.  
  46.     
  47.     def test_union_empty(self):
  48.         result = empty_set | self.set
  49.         self.assertEqual(result, self.dup)
  50.  
  51.     
  52.     def test_self_intersection(self):
  53.         result = self.set & self.set
  54.         self.assertEqual(result, self.dup)
  55.  
  56.     
  57.     def test_empty_intersection(self):
  58.         result = self.set & empty_set
  59.         self.assertEqual(result, empty_set)
  60.  
  61.     
  62.     def test_intersection_empty(self):
  63.         result = empty_set & self.set
  64.         self.assertEqual(result, empty_set)
  65.  
  66.     
  67.     def test_self_symmetric_difference(self):
  68.         result = self.set ^ self.set
  69.         self.assertEqual(result, empty_set)
  70.  
  71.     
  72.     def checkempty_symmetric_difference(self):
  73.         result = self.set ^ empty_set
  74.         self.assertEqual(result, self.set)
  75.  
  76.     
  77.     def test_self_difference(self):
  78.         result = self.set - self.set
  79.         self.assertEqual(result, empty_set)
  80.  
  81.     
  82.     def test_empty_difference(self):
  83.         result = self.set - empty_set
  84.         self.assertEqual(result, self.dup)
  85.  
  86.     
  87.     def test_empty_difference_rev(self):
  88.         result = empty_set - self.set
  89.         self.assertEqual(result, empty_set)
  90.  
  91.     
  92.     def test_iteration(self):
  93.         for v in self.set:
  94.             self.assert_(v in self.values)
  95.         
  96.  
  97.     
  98.     def test_pickling(self):
  99.         p = pickle.dumps(self.set)
  100.         copy = pickle.loads(p)
  101.         self.assertEqual(self.set, copy, '%s != %s' % (self.set, copy))
  102.  
  103.  
  104.  
  105. class TestBasicOpsEmpty(TestBasicOps):
  106.     
  107.     def setUp(self):
  108.         self.case = 'empty set'
  109.         self.values = []
  110.         self.set = Set(self.values)
  111.         self.dup = Set(self.values)
  112.         self.length = 0
  113.         self.repr = 'Set([])'
  114.  
  115.  
  116.  
  117. class TestBasicOpsSingleton(TestBasicOps):
  118.     
  119.     def setUp(self):
  120.         self.case = 'unit set (number)'
  121.         self.values = [
  122.             3]
  123.         self.set = Set(self.values)
  124.         self.dup = Set(self.values)
  125.         self.length = 1
  126.         self.repr = 'Set([3])'
  127.  
  128.     
  129.     def test_in(self):
  130.         self.failUnless(3 in self.set)
  131.  
  132.     
  133.     def test_not_in(self):
  134.         self.failUnless(2 not in self.set)
  135.  
  136.  
  137.  
  138. class TestBasicOpsTuple(TestBasicOps):
  139.     
  140.     def setUp(self):
  141.         self.case = 'unit set (tuple)'
  142.         self.values = [
  143.             (0, 'zero')]
  144.         self.set = Set(self.values)
  145.         self.dup = Set(self.values)
  146.         self.length = 1
  147.         self.repr = "Set([(0, 'zero')])"
  148.  
  149.     
  150.     def test_in(self):
  151.         self.failUnless((0, 'zero') in self.set)
  152.  
  153.     
  154.     def test_not_in(self):
  155.         self.failUnless(9 not in self.set)
  156.  
  157.  
  158.  
  159. class TestBasicOpsTriple(TestBasicOps):
  160.     
  161.     def setUp(self):
  162.         self.case = 'triple set'
  163.         self.values = [
  164.             0,
  165.             'zero',
  166.             operator.add]
  167.         self.set = Set(self.values)
  168.         self.dup = Set(self.values)
  169.         self.length = 3
  170.         self.repr = None
  171.  
  172.  
  173.  
  174. def baditer():
  175.     raise TypeError
  176.     yield True
  177.  
  178.  
  179. def gooditer():
  180.     yield True
  181.  
  182.  
  183. class TestExceptionPropagation(unittest.TestCase):
  184.     '''SF 628246:  Set constructor should not trap iterator TypeErrors'''
  185.     
  186.     def test_instanceWithException(self):
  187.         self.assertRaises(TypeError, Set, baditer())
  188.  
  189.     
  190.     def test_instancesWithoutException(self):
  191.         Set([
  192.             1,
  193.             2,
  194.             3])
  195.         Set((1, 2, 3))
  196.         Set({
  197.             'one': 1,
  198.             'two': 2,
  199.             'three': 3 })
  200.         Set(xrange(3))
  201.         Set('abc')
  202.         Set(gooditer())
  203.  
  204.  
  205.  
  206. class TestSetOfSets(unittest.TestCase):
  207.     
  208.     def test_constructor(self):
  209.         inner = Set([
  210.             1])
  211.         outer = Set([
  212.             inner])
  213.         element = outer.pop()
  214.         self.assertEqual(type(element), ImmutableSet)
  215.         outer.add(inner)
  216.         outer.remove(inner)
  217.         self.assertEqual(outer, Set())
  218.         outer.discard(inner)
  219.  
  220.  
  221.  
  222. class TestBinaryOps(unittest.TestCase):
  223.     
  224.     def setUp(self):
  225.         self.set = Set((2, 4, 6))
  226.  
  227.     
  228.     def test_eq(self):
  229.         self.assertEqual(self.set, Set({
  230.             2: 1,
  231.             4: 3,
  232.             6: 5 }))
  233.  
  234.     
  235.     def test_union_subset(self):
  236.         result = self.set | Set([
  237.             2])
  238.         self.assertEqual(result, Set((2, 4, 6)))
  239.  
  240.     
  241.     def test_union_superset(self):
  242.         result = self.set | Set([
  243.             2,
  244.             4,
  245.             6,
  246.             8])
  247.         self.assertEqual(result, Set([
  248.             2,
  249.             4,
  250.             6,
  251.             8]))
  252.  
  253.     
  254.     def test_union_overlap(self):
  255.         result = self.set | Set([
  256.             3,
  257.             4,
  258.             5])
  259.         self.assertEqual(result, Set([
  260.             2,
  261.             3,
  262.             4,
  263.             5,
  264.             6]))
  265.  
  266.     
  267.     def test_union_non_overlap(self):
  268.         result = self.set | Set([
  269.             8])
  270.         self.assertEqual(result, Set([
  271.             2,
  272.             4,
  273.             6,
  274.             8]))
  275.  
  276.     
  277.     def test_intersection_subset(self):
  278.         result = self.set & Set((2, 4))
  279.         self.assertEqual(result, Set((2, 4)))
  280.  
  281.     
  282.     def test_intersection_superset(self):
  283.         result = self.set & Set([
  284.             2,
  285.             4,
  286.             6,
  287.             8])
  288.         self.assertEqual(result, Set([
  289.             2,
  290.             4,
  291.             6]))
  292.  
  293.     
  294.     def test_intersection_overlap(self):
  295.         result = self.set & Set([
  296.             3,
  297.             4,
  298.             5])
  299.         self.assertEqual(result, Set([
  300.             4]))
  301.  
  302.     
  303.     def test_intersection_non_overlap(self):
  304.         result = self.set & Set([
  305.             8])
  306.         self.assertEqual(result, empty_set)
  307.  
  308.     
  309.     def test_sym_difference_subset(self):
  310.         result = self.set ^ Set((2, 4))
  311.         self.assertEqual(result, Set([
  312.             6]))
  313.  
  314.     
  315.     def test_sym_difference_superset(self):
  316.         result = self.set ^ Set((2, 4, 6, 8))
  317.         self.assertEqual(result, Set([
  318.             8]))
  319.  
  320.     
  321.     def test_sym_difference_overlap(self):
  322.         result = self.set ^ Set((3, 4, 5))
  323.         self.assertEqual(result, Set([
  324.             2,
  325.             3,
  326.             5,
  327.             6]))
  328.  
  329.     
  330.     def test_sym_difference_non_overlap(self):
  331.         result = self.set ^ Set([
  332.             8])
  333.         self.assertEqual(result, Set([
  334.             2,
  335.             4,
  336.             6,
  337.             8]))
  338.  
  339.     
  340.     def test_cmp(self):
  341.         a = Set('a')
  342.         b = Set('b')
  343.         self.assertRaises(TypeError, cmp, a, b)
  344.         self.assertEqual(cmp(a, a), 0)
  345.         self.assertRaises(TypeError, cmp, a, 12)
  346.         self.assertRaises(TypeError, cmp, 'abc', a)
  347.  
  348.     
  349.     def test_inplace_on_self(self):
  350.         t = self.set.copy()
  351.         t |= t
  352.         self.assertEqual(t, self.set)
  353.         t &= t
  354.         self.assertEqual(t, self.set)
  355.         t -= t
  356.         self.assertEqual(len(t), 0)
  357.         t = self.set.copy()
  358.         t ^= t
  359.         self.assertEqual(len(t), 0)
  360.  
  361.  
  362.  
  363. class TestUpdateOps(unittest.TestCase):
  364.     
  365.     def setUp(self):
  366.         self.set = Set((2, 4, 6))
  367.  
  368.     
  369.     def test_union_subset(self):
  370.         self.set |= Set([
  371.             2])
  372.         self.assertEqual(self.set, Set((2, 4, 6)))
  373.  
  374.     
  375.     def test_union_superset(self):
  376.         self.set |= Set([
  377.             2,
  378.             4,
  379.             6,
  380.             8])
  381.         self.assertEqual(self.set, Set([
  382.             2,
  383.             4,
  384.             6,
  385.             8]))
  386.  
  387.     
  388.     def test_union_overlap(self):
  389.         self.set |= Set([
  390.             3,
  391.             4,
  392.             5])
  393.         self.assertEqual(self.set, Set([
  394.             2,
  395.             3,
  396.             4,
  397.             5,
  398.             6]))
  399.  
  400.     
  401.     def test_union_non_overlap(self):
  402.         self.set |= Set([
  403.             8])
  404.         self.assertEqual(self.set, Set([
  405.             2,
  406.             4,
  407.             6,
  408.             8]))
  409.  
  410.     
  411.     def test_union_method_call(self):
  412.         self.set.union_update(Set([
  413.             3,
  414.             4,
  415.             5]))
  416.         self.assertEqual(self.set, Set([
  417.             2,
  418.             3,
  419.             4,
  420.             5,
  421.             6]))
  422.  
  423.     
  424.     def test_intersection_subset(self):
  425.         self.set &= Set((2, 4))
  426.         self.assertEqual(self.set, Set((2, 4)))
  427.  
  428.     
  429.     def test_intersection_superset(self):
  430.         self.set &= Set([
  431.             2,
  432.             4,
  433.             6,
  434.             8])
  435.         self.assertEqual(self.set, Set([
  436.             2,
  437.             4,
  438.             6]))
  439.  
  440.     
  441.     def test_intersection_overlap(self):
  442.         self.set &= Set([
  443.             3,
  444.             4,
  445.             5])
  446.         self.assertEqual(self.set, Set([
  447.             4]))
  448.  
  449.     
  450.     def test_intersection_non_overlap(self):
  451.         self.set &= Set([
  452.             8])
  453.         self.assertEqual(self.set, empty_set)
  454.  
  455.     
  456.     def test_intersection_method_call(self):
  457.         self.set.intersection_update(Set([
  458.             3,
  459.             4,
  460.             5]))
  461.         self.assertEqual(self.set, Set([
  462.             4]))
  463.  
  464.     
  465.     def test_sym_difference_subset(self):
  466.         self.set ^= Set((2, 4))
  467.         self.assertEqual(self.set, Set([
  468.             6]))
  469.  
  470.     
  471.     def test_sym_difference_superset(self):
  472.         self.set ^= Set((2, 4, 6, 8))
  473.         self.assertEqual(self.set, Set([
  474.             8]))
  475.  
  476.     
  477.     def test_sym_difference_overlap(self):
  478.         self.set ^= Set((3, 4, 5))
  479.         self.assertEqual(self.set, Set([
  480.             2,
  481.             3,
  482.             5,
  483.             6]))
  484.  
  485.     
  486.     def test_sym_difference_non_overlap(self):
  487.         self.set ^= Set([
  488.             8])
  489.         self.assertEqual(self.set, Set([
  490.             2,
  491.             4,
  492.             6,
  493.             8]))
  494.  
  495.     
  496.     def test_sym_difference_method_call(self):
  497.         self.set.symmetric_difference_update(Set([
  498.             3,
  499.             4,
  500.             5]))
  501.         self.assertEqual(self.set, Set([
  502.             2,
  503.             3,
  504.             5,
  505.             6]))
  506.  
  507.     
  508.     def test_difference_subset(self):
  509.         self.set -= Set((2, 4))
  510.         self.assertEqual(self.set, Set([
  511.             6]))
  512.  
  513.     
  514.     def test_difference_superset(self):
  515.         self.set -= Set((2, 4, 6, 8))
  516.         self.assertEqual(self.set, Set([]))
  517.  
  518.     
  519.     def test_difference_overlap(self):
  520.         self.set -= Set((3, 4, 5))
  521.         self.assertEqual(self.set, Set([
  522.             2,
  523.             6]))
  524.  
  525.     
  526.     def test_difference_non_overlap(self):
  527.         self.set -= Set([
  528.             8])
  529.         self.assertEqual(self.set, Set([
  530.             2,
  531.             4,
  532.             6]))
  533.  
  534.     
  535.     def test_difference_method_call(self):
  536.         self.set.difference_update(Set([
  537.             3,
  538.             4,
  539.             5]))
  540.         self.assertEqual(self.set, Set([
  541.             2,
  542.             6]))
  543.  
  544.  
  545.  
  546. class TestMutate(unittest.TestCase):
  547.     
  548.     def setUp(self):
  549.         self.values = [
  550.             'a',
  551.             'b',
  552.             'c']
  553.         self.set = Set(self.values)
  554.  
  555.     
  556.     def test_add_present(self):
  557.         self.set.add('c')
  558.         self.assertEqual(self.set, Set('abc'))
  559.  
  560.     
  561.     def test_add_absent(self):
  562.         self.set.add('d')
  563.         self.assertEqual(self.set, Set('abcd'))
  564.  
  565.     
  566.     def test_add_until_full(self):
  567.         tmp = Set()
  568.         expected_len = 0
  569.         for v in self.values:
  570.             tmp.add(v)
  571.             expected_len += 1
  572.             self.assertEqual(len(tmp), expected_len)
  573.         
  574.         self.assertEqual(tmp, self.set)
  575.  
  576.     
  577.     def test_remove_present(self):
  578.         self.set.remove('b')
  579.         self.assertEqual(self.set, Set('ac'))
  580.  
  581.     
  582.     def test_remove_absent(self):
  583.         
  584.         try:
  585.             self.set.remove('d')
  586.             self.fail('Removing missing element should have raised LookupError')
  587.         except LookupError:
  588.             pass
  589.  
  590.  
  591.     
  592.     def test_remove_until_empty(self):
  593.         expected_len = len(self.set)
  594.         for v in self.values:
  595.             self.set.remove(v)
  596.             expected_len -= 1
  597.             self.assertEqual(len(self.set), expected_len)
  598.         
  599.  
  600.     
  601.     def test_discard_present(self):
  602.         self.set.discard('c')
  603.         self.assertEqual(self.set, Set('ab'))
  604.  
  605.     
  606.     def test_discard_absent(self):
  607.         self.set.discard('d')
  608.         self.assertEqual(self.set, Set('abc'))
  609.  
  610.     
  611.     def test_clear(self):
  612.         self.set.clear()
  613.         self.assertEqual(len(self.set), 0)
  614.  
  615.     
  616.     def test_pop(self):
  617.         popped = { }
  618.         while self.set:
  619.             popped[self.set.pop()] = None
  620.         self.assertEqual(len(popped), len(self.values))
  621.         for v in self.values:
  622.             self.failUnless(v in popped)
  623.         
  624.  
  625.     
  626.     def test_update_empty_tuple(self):
  627.         self.set.union_update(())
  628.         self.assertEqual(self.set, Set(self.values))
  629.  
  630.     
  631.     def test_update_unit_tuple_overlap(self):
  632.         self.set.union_update(('a',))
  633.         self.assertEqual(self.set, Set(self.values))
  634.  
  635.     
  636.     def test_update_unit_tuple_non_overlap(self):
  637.         self.set.union_update(('a', 'z'))
  638.         self.assertEqual(self.set, Set(self.values + [
  639.             'z']))
  640.  
  641.  
  642.  
  643. class TestSubsets(unittest.TestCase):
  644.     case2method = {
  645.         '<=': 'issubset',
  646.         '>=': 'issuperset' }
  647.     reverse = {
  648.         '==': '==',
  649.         '!=': '!=',
  650.         '<': '>',
  651.         '>': '<',
  652.         '<=': '>=',
  653.         '>=': '<=' }
  654.     
  655.     def test_issubset(self):
  656.         x = self.left
  657.         y = self.right
  658.         for case in ('!=', '==', '<', '<=', '>', '>='):
  659.             expected = case in self.cases
  660.             result = eval('x' + case + 'y', locals())
  661.             self.assertEqual(result, expected)
  662.             if case in TestSubsets.case2method:
  663.                 method = getattr(x, TestSubsets.case2method[case])
  664.                 result = method(y)
  665.                 self.assertEqual(result, expected)
  666.             
  667.             rcase = TestSubsets.reverse[case]
  668.             result = eval('y' + rcase + 'x', locals())
  669.             self.assertEqual(result, expected)
  670.             if rcase in TestSubsets.case2method:
  671.                 method = getattr(y, TestSubsets.case2method[rcase])
  672.                 result = method(x)
  673.                 self.assertEqual(result, expected)
  674.                 continue
  675.         
  676.  
  677.  
  678.  
  679. class TestSubsetEqualEmpty(TestSubsets):
  680.     left = Set()
  681.     right = Set()
  682.     name = 'both empty'
  683.     cases = ('==', '<=', '>=')
  684.  
  685.  
  686. class TestSubsetEqualNonEmpty(TestSubsets):
  687.     left = Set([
  688.         1,
  689.         2])
  690.     right = Set([
  691.         1,
  692.         2])
  693.     name = 'equal pair'
  694.     cases = ('==', '<=', '>=')
  695.  
  696.  
  697. class TestSubsetEmptyNonEmpty(TestSubsets):
  698.     left = Set()
  699.     right = Set([
  700.         1,
  701.         2])
  702.     name = 'one empty, one non-empty'
  703.     cases = ('!=', '<', '<=')
  704.  
  705.  
  706. class TestSubsetPartial(TestSubsets):
  707.     left = Set([
  708.         1])
  709.     right = Set([
  710.         1,
  711.         2])
  712.     name = 'one a non-empty proper subset of other'
  713.     cases = ('!=', '<', '<=')
  714.  
  715.  
  716. class TestSubsetNonOverlap(TestSubsets):
  717.     left = Set([
  718.         1])
  719.     right = Set([
  720.         2])
  721.     name = 'neither empty, neither contains'
  722.     cases = '!='
  723.  
  724.  
  725. class TestOnlySetsInBinaryOps(unittest.TestCase):
  726.     
  727.     def test_eq_ne(self):
  728.         self.assertEqual(self.other == self.set, False)
  729.         self.assertEqual(self.set == self.other, False)
  730.         self.assertEqual(self.other != self.set, True)
  731.         self.assertEqual(self.set != self.other, True)
  732.  
  733.     
  734.     def test_ge_gt_le_lt(self):
  735.         self.assertRaises(TypeError, (lambda : self.set < self.other))
  736.         self.assertRaises(TypeError, (lambda : self.set <= self.other))
  737.         self.assertRaises(TypeError, (lambda : self.set > self.other))
  738.         self.assertRaises(TypeError, (lambda : self.set >= self.other))
  739.         self.assertRaises(TypeError, (lambda : self.other < self.set))
  740.         self.assertRaises(TypeError, (lambda : self.other <= self.set))
  741.         self.assertRaises(TypeError, (lambda : self.other > self.set))
  742.         self.assertRaises(TypeError, (lambda : self.other >= self.set))
  743.  
  744.     
  745.     def test_union_update_operator(self):
  746.         
  747.         try:
  748.             self.set |= self.other
  749.         except TypeError:
  750.             pass
  751.  
  752.         self.fail('expected TypeError')
  753.  
  754.     
  755.     def test_union_update(self):
  756.         if self.otherIsIterable:
  757.             self.set.union_update(self.other)
  758.         else:
  759.             self.assertRaises(TypeError, self.set.union_update, self.other)
  760.  
  761.     
  762.     def test_union(self):
  763.         self.assertRaises(TypeError, (lambda : self.set | self.other))
  764.         self.assertRaises(TypeError, (lambda : self.other | self.set))
  765.         if self.otherIsIterable:
  766.             self.set.union(self.other)
  767.         else:
  768.             self.assertRaises(TypeError, self.set.union, self.other)
  769.  
  770.     
  771.     def test_intersection_update_operator(self):
  772.         
  773.         try:
  774.             self.set &= self.other
  775.         except TypeError:
  776.             pass
  777.  
  778.         self.fail('expected TypeError')
  779.  
  780.     
  781.     def test_intersection_update(self):
  782.         if self.otherIsIterable:
  783.             self.set.intersection_update(self.other)
  784.         else:
  785.             self.assertRaises(TypeError, self.set.intersection_update, self.other)
  786.  
  787.     
  788.     def test_intersection(self):
  789.         self.assertRaises(TypeError, (lambda : self.set & self.other))
  790.         self.assertRaises(TypeError, (lambda : self.other & self.set))
  791.         if self.otherIsIterable:
  792.             self.set.intersection(self.other)
  793.         else:
  794.             self.assertRaises(TypeError, self.set.intersection, self.other)
  795.  
  796.     
  797.     def test_sym_difference_update_operator(self):
  798.         
  799.         try:
  800.             self.set ^= self.other
  801.         except TypeError:
  802.             pass
  803.  
  804.         self.fail('expected TypeError')
  805.  
  806.     
  807.     def test_sym_difference_update(self):
  808.         if self.otherIsIterable:
  809.             self.set.symmetric_difference_update(self.other)
  810.         else:
  811.             self.assertRaises(TypeError, self.set.symmetric_difference_update, self.other)
  812.  
  813.     
  814.     def test_sym_difference(self):
  815.         self.assertRaises(TypeError, (lambda : self.set ^ self.other))
  816.         self.assertRaises(TypeError, (lambda : self.other ^ self.set))
  817.         if self.otherIsIterable:
  818.             self.set.symmetric_difference(self.other)
  819.         else:
  820.             self.assertRaises(TypeError, self.set.symmetric_difference, self.other)
  821.  
  822.     
  823.     def test_difference_update_operator(self):
  824.         
  825.         try:
  826.             self.set -= self.other
  827.         except TypeError:
  828.             pass
  829.  
  830.         self.fail('expected TypeError')
  831.  
  832.     
  833.     def test_difference_update(self):
  834.         if self.otherIsIterable:
  835.             self.set.difference_update(self.other)
  836.         else:
  837.             self.assertRaises(TypeError, self.set.difference_update, self.other)
  838.  
  839.     
  840.     def test_difference(self):
  841.         self.assertRaises(TypeError, (lambda : self.set - self.other))
  842.         self.assertRaises(TypeError, (lambda : self.other - self.set))
  843.         if self.otherIsIterable:
  844.             self.set.difference(self.other)
  845.         else:
  846.             self.assertRaises(TypeError, self.set.difference, self.other)
  847.  
  848.  
  849.  
  850. class TestOnlySetsNumeric(TestOnlySetsInBinaryOps):
  851.     
  852.     def setUp(self):
  853.         self.set = Set((1, 2, 3))
  854.         self.other = 19
  855.         self.otherIsIterable = False
  856.  
  857.  
  858.  
  859. class TestOnlySetsDict(TestOnlySetsInBinaryOps):
  860.     
  861.     def setUp(self):
  862.         self.set = Set((1, 2, 3))
  863.         self.other = {
  864.             1: 2,
  865.             3: 4 }
  866.         self.otherIsIterable = True
  867.  
  868.  
  869.  
  870. class TestOnlySetsOperator(TestOnlySetsInBinaryOps):
  871.     
  872.     def setUp(self):
  873.         self.set = Set((1, 2, 3))
  874.         self.other = operator.add
  875.         self.otherIsIterable = False
  876.  
  877.  
  878.  
  879. class TestOnlySetsTuple(TestOnlySetsInBinaryOps):
  880.     
  881.     def setUp(self):
  882.         self.set = Set((1, 2, 3))
  883.         self.other = (2, 4, 6)
  884.         self.otherIsIterable = True
  885.  
  886.  
  887.  
  888. class TestOnlySetsString(TestOnlySetsInBinaryOps):
  889.     
  890.     def setUp(self):
  891.         self.set = Set((1, 2, 3))
  892.         self.other = 'abc'
  893.         self.otherIsIterable = True
  894.  
  895.  
  896.  
  897. class TestOnlySetsGenerator(TestOnlySetsInBinaryOps):
  898.     
  899.     def setUp(self):
  900.         
  901.         def gen():
  902.             for i in xrange(0, 10, 2):
  903.                 yield i
  904.             
  905.  
  906.         self.set = Set((1, 2, 3))
  907.         self.other = gen()
  908.         self.otherIsIterable = True
  909.  
  910.  
  911.  
  912. class TestOnlySetsofSets(TestOnlySetsInBinaryOps):
  913.     
  914.     def setUp(self):
  915.         self.set = Set((1, 2, 3))
  916.         self.other = [
  917.             Set('ab'),
  918.             ImmutableSet('cd')]
  919.         self.otherIsIterable = True
  920.  
  921.  
  922.  
  923. class TestCopying(unittest.TestCase):
  924.     
  925.     def test_copy(self):
  926.         dup = self.set.copy()
  927.         dup_list = list(dup)
  928.         dup_list.sort()
  929.         set_list = list(self.set)
  930.         set_list.sort()
  931.         self.assertEqual(len(dup_list), len(set_list))
  932.         for i in range(len(dup_list)):
  933.             self.failUnless(dup_list[i] is set_list[i])
  934.         
  935.  
  936.     
  937.     def test_deep_copy(self):
  938.         dup = copy.deepcopy(self.set)
  939.         dup_list = list(dup)
  940.         dup_list.sort()
  941.         set_list = list(self.set)
  942.         set_list.sort()
  943.         self.assertEqual(len(dup_list), len(set_list))
  944.         for i in range(len(dup_list)):
  945.             self.assertEqual(dup_list[i], set_list[i])
  946.         
  947.  
  948.  
  949.  
  950. class TestCopyingEmpty(TestCopying):
  951.     
  952.     def setUp(self):
  953.         self.set = Set()
  954.  
  955.  
  956.  
  957. class TestCopyingSingleton(TestCopying):
  958.     
  959.     def setUp(self):
  960.         self.set = Set([
  961.             'hello'])
  962.  
  963.  
  964.  
  965. class TestCopyingTriple(TestCopying):
  966.     
  967.     def setUp(self):
  968.         self.set = Set([
  969.             'zero',
  970.             0,
  971.             None])
  972.  
  973.  
  974.  
  975. class TestCopyingTuple(TestCopying):
  976.     
  977.     def setUp(self):
  978.         self.set = Set([
  979.             (1, 2)])
  980.  
  981.  
  982.  
  983. class TestCopyingNested(TestCopying):
  984.     
  985.     def setUp(self):
  986.         self.set = Set([
  987.             ((1, 2), (3, 4))])
  988.  
  989.  
  990.  
  991. class TestIdentities(unittest.TestCase):
  992.     
  993.     def setUp(self):
  994.         self.a = []([ random.randrange(100) for i in xrange(50) ])
  995.         self.b = []([ random.randrange(100) for i in xrange(50) ])
  996.  
  997.     
  998.     def test_binopsVsSubsets(self):
  999.         a = self.a
  1000.         b = self.b
  1001.         self.assert_(a - b <= a)
  1002.         self.assert_(b - a <= b)
  1003.         self.assert_(a & b <= a)
  1004.         self.assert_(a & b <= b)
  1005.         self.assert_(a | b >= a)
  1006.         self.assert_(a | b >= b)
  1007.         self.assert_(a ^ b <= a | b)
  1008.  
  1009.     
  1010.     def test_commutativity(self):
  1011.         a = self.a
  1012.         b = self.b
  1013.         self.assertEqual(a & b, b & a)
  1014.         self.assertEqual(a | b, b | a)
  1015.         self.assertEqual(a ^ b, b ^ a)
  1016.         if a != b:
  1017.             self.assertNotEqual(a - b, b - a)
  1018.         
  1019.  
  1020.     
  1021.     def test_reflexsive_relations(self):
  1022.         a = self.a
  1023.         zero = Set()
  1024.         self.assertEqual(a ^ a, zero)
  1025.         self.assertEqual(a - a, zero)
  1026.         self.assertEqual(a | a, a)
  1027.         self.assertEqual(a & a, a)
  1028.         self.assert_(a <= a)
  1029.         self.assert_(a >= a)
  1030.         self.assert_(a == a)
  1031.  
  1032.     
  1033.     def test_summations(self):
  1034.         a = self.a
  1035.         b = self.b
  1036.         self.assertEqual(a - b | a & b | b - a, a | b)
  1037.         self.assertEqual(a & b | a ^ b, a | b)
  1038.         self.assertEqual(a | b - a, a | b)
  1039.         self.assertEqual(a - b | b, a | b)
  1040.         self.assertEqual(a - b | a & b, a)
  1041.         self.assertEqual(b - a | a & b, b)
  1042.         self.assertEqual(a - b | b - a, a ^ b)
  1043.  
  1044.     
  1045.     def test_exclusion(self):
  1046.         a = self.a
  1047.         b = self.b
  1048.         zero = Set()
  1049.         self.assertEqual(a - b & b, zero)
  1050.         self.assertEqual(b - a & a, zero)
  1051.         self.assertEqual(a & b & (a ^ b), zero)
  1052.  
  1053.     
  1054.     def test_cardinality_relations(self):
  1055.         a = self.a
  1056.         b = self.b
  1057.         self.assertEqual(len(a), len(a - b) + len(a & b))
  1058.         self.assertEqual(len(b), len(b - a) + len(a & b))
  1059.         self.assertEqual(len(a ^ b), len(a - b) + len(b - a))
  1060.         self.assertEqual(len(a | b), len(a - b) + len(a & b) + len(b - a))
  1061.         self.assertEqual(len(a ^ b) + len(a & b), len(a | b))
  1062.  
  1063.  
  1064. libreftest = "\nExample from the Library Reference:  Doc/lib/libsets.tex\n\n>>> from sets import Set as Base  # override _repr to get sorted output\n>>> class Set(Base):\n...     def _repr(self):\n...         return Base._repr(self, sorted=True)\n>>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])\n>>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])\n>>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])\n>>> employees = engineers | programmers | managers           # union\n>>> engineering_management = engineers & managers            # intersection\n>>> fulltime_management = managers - engineers - programmers # difference\n>>> engineers.add('Marvin')\n>>> print engineers\nSet(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])\n>>> employees.issuperset(engineers)           # superset test\nFalse\n>>> employees.union_update(engineers)         # update from another set\n>>> employees.issuperset(engineers)\nTrue\n>>> for group in [engineers, programmers, managers, employees]:\n...     group.discard('Susan')                # unconditionally remove element\n...     print group\n...\nSet(['Jack', 'Jane', 'Janice', 'John', 'Marvin'])\nSet(['Jack', 'Janice', 'Sam'])\nSet(['Jack', 'Jane', 'Zack'])\nSet(['Jack', 'Jane', 'Janice', 'John', 'Marvin', 'Sam', 'Zack'])\n"
  1065. __test__ = {
  1066.     'libreftest': libreftest }
  1067.  
  1068. def test_main(verbose = None):
  1069.     import test_sets as test_sets
  1070.     import doctest as doctest
  1071.     test_support.run_unittest(TestSetOfSets, TestExceptionPropagation, TestBasicOpsEmpty, TestBasicOpsSingleton, TestBasicOpsTuple, TestBasicOpsTriple, TestBinaryOps, TestUpdateOps, TestMutate, TestSubsetEqualEmpty, TestSubsetEqualNonEmpty, TestSubsetEmptyNonEmpty, TestSubsetPartial, TestSubsetNonOverlap, TestOnlySetsNumeric, TestOnlySetsDict, TestOnlySetsOperator, TestOnlySetsTuple, TestOnlySetsString, TestOnlySetsGenerator, TestOnlySetsofSets, TestCopyingEmpty, TestCopyingSingleton, TestCopyingTriple, TestCopyingTuple, TestCopyingNested, TestIdentities, doctest.DocTestSuite(test_sets))
  1072.  
  1073. if __name__ == '__main__':
  1074.     test_main(verbose = True)
  1075.  
  1076.